using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace de_kleintjesmarkt
{
   public partial class CtlPlaatsAdvertentie : System.Web.UI.UserControl
   {
      private Dictionary<int, string> kranten;
      
      protected void Page_Load(object sender, EventArgs e)
      {         
         FileReader reader = new FileReader();
         if (!Page.IsPostBack)
         {
            kranten = reader.ReadKranten();
            DDListKranten.DataSource = kranten;
            DDListKranten.DataBind();
         }
      }

      protected void butVersturen_Click(object sender, EventArgs e)
      {
         byte[] image = (byte[]) Session["afbeelding"];
         
          PlaatsAdvertentieInLeidschDagblad.PlaatsAdvertentieInLeidschDagblad adServer = new PlaatsAdvertentieInLeidschDagblad.PlaatsAdvertentieInLeidschDagblad();
         //KlantInfo.Klant klant = (KlantInfo.Klant)Session["klant"];  // Object in session was of type 'RegisterUser.Klant'
         //KlantInfo.Klant klant = (KlantInfo.Klant)Session["klant"];
          string email = Session["Klant"].ToString();

          KlantInfo.KlantenInfo info = new KlantInfo.KlantenInfo();
          PlaatsAdvertentieInLeidschDagblad.Klant klant = new PlaatsAdvertentieInLeidschDagblad.Klant();
          
          KlantInfo.Klant infoklant = info.GetKlantInfo(email);

          //Freaking dirty but it works And is needed because each service has some different klant class.
          klant.Address = infoklant.Address;
          klant.City = infoklant.City;
          klant.Email = infoklant.Email;
          klant.Firstname = infoklant.Firstname;
          klant.House_number = infoklant.House_number;
          klant.House_number_add = infoklant.House_number_add;
          klant.Lastname = infoklant.Lastname;
          klant.Member_number = infoklant.Member_number;
          klant.Name_addition = infoklant.Name_addition;
          klant.Postal_code = infoklant.Postal_code;

          int krantnr = 0;
          string tmp = Regex.Replace(DDListKranten.SelectedValue.ToString(),
            @"^\[(\d*?),.*?\]$", "$1");
          krantnr = Int32.Parse(tmp);

         if (klant != null)
         {
             bool result = adServer.PlaceAd(klant, krantnr, image, tbAdvertentieTekst.Text);
         }
      }
    
      protected void butTarief_Click(object sender, EventArgs e)
      {
        byte[] afbeelding = fileUpload.FileBytes;
        Session["afbeelding"] = afbeelding;
        PlaatsAdvertentieInLeidschDagblad.PlaatsAdvertentieInLeidschDagblad adServer = new PlaatsAdvertentieInLeidschDagblad.PlaatsAdvertentieInLeidschDagblad();
        double result = adServer.CalculateCost(afbeelding.Length, tbAdvertentieTekst.Text.Length);
        litResult.Text = result.ToString();

        butPlaatsAdvertentie.Enabled = true;
        butTarief.Enabled = false;
        tbAdvertentieTekst.Enabled = false;
        fileUpload.Enabled = false;        
      }

      protected void butCancel_Click(object sender, EventArgs e)
      {
         butPlaatsAdvertentie.Enabled = false;
         butTarief.Enabled = true;
         tbAdvertentieTekst.Enabled = true;
         fileUpload.Enabled = true;
      }
   }
}